init("0", 2)
luaExitIfCall(true)

SCREEN_HEIGHT, SCREEN_WIDTH = getScreenSize();
COCAPP_IDENTIFIER = "com.supercell.magic"

function cordFromTouch5(x, y)
    return SCREEN_WIDTH * x / 1136, SCREEN_HEIGHT * y / 640
end

function cordFromiPadMini2(x, y)
    return SCREEN_WIDTH * x / 2048, SCREEN_HEIGHT * y / 1536
end

function cordFromiPhone4(x, y)
    return SCREEN_WIDTH * x / 960, SCREEN_HEIGHT * y / 640
end

-- function getMultiColorParam(colors, dx, dy)
--     local param = dx .. "|0|" .. string.format("0x%06X", colors[2]) .. ",0|" .. dy .. "|" .. string.format("0x%06X", colors[3]) .. "," .. dx .. "|" .. dy .. "|" .. string.format("0x%06X", colors[4])
--     return string.format("0x%06X", colors[1]), param
-- end

-- function findDonateButton(x1, y1, x2, y2)
--     local dx, dy = math.floor(0.5 + SCREEN_WIDTH * 0.0185), math.floor(0.5 + SCREEN_WIDTH * 0.0185)
--     local colors = { 0x030303, 0xeff7df, 0x75b82b, 0xaed955 }
--     local color, param = getMultiColorParam(colors, dx, dy)
--     local tmpX, tmpY = findMultiColorInRegionFuzzy(color, param, 80, x1, y1, x2, y2)
--     if tmpX ~= -1 and tmpY ~= -1 then
--         return tmpX, tmpY
--     else
--     	return -1, -1
--     end
-- end

function isRandomRequset(startX, startY)
	-- local sizeWidth, sizeHeight = math.floor(0.5 + SCREEN_WIDTH * 0.0317), math.floor(0.5 + SCREEN_HEIGHT * 0.0297)
	local sizeWidth, sizeHeight = math.floor(0.5 + SCREEN_WIDTH * 0.0135), math.floor(0.5 + SCREEN_HEIGHT * 0.0182)
	-- local sizeWidth, sizeHeight = math.floor(0.5 + SCREEN_WIDTH * 0.0385), math.floor(0.5 + SCREEN_HEIGHT * 0.0297)
	local grayCount1, size1 = 0, math.floor(sizeWidth / 2) * sizeHeight
	local grayCount2, size2 = 0, sizeWidth * sizeHeight - size1
	keepScreen(true)
	for y = 1,sizeHeight do
		for x = 1,sizeWidth do
			local r, g, b = getColorRGB(startX + x, startY + y)
			if (r + g + b - 482) < 0 then
				if (x < math.floor(sizeWidth / 2)) then
					grayCount1 = grayCount1 + 1
				else
					grayCount2 = grayCount2 + 1
				end
			end
		end
	end
	keepScreen(false)
	local k1 = grayCount1 / size1;
	local k2 = grayCount2 / size2;
	-- wLog("color", "gray1:" .. grayCount1 .. "  all1:" .. size1 .. "  k1:" .. k1)
	-- wLog("color", "gray2:" .. grayCount2 .. "  all2:" .. size2 .. "  k2:" .. k2)
	if math.abs(k1 - 0.563776) < 0.005 and math.abs(k2 - 0.701531) < 0.005 then
		dialog("随", 0)
		return true
	elseif math.abs(k1 - 0.622449) < 0.005 and math.abs(k2 - 0.752551) < 0.005 then
		dialog("救", 0)
		return true
	else
		return false
	end
end	

mSleep(500)
initLog("color", 0)
-- x, y = cordFromTouch5(40, 231)
x, y = cordFromiPadMini2(82, 348)
-- x, y = cordFromiPhone4(40, 231)
isRandomRequset(x, y)
closeLog("color")